# -*- coding:utf-8 -*-
__doc__ = '待我审批页面'

import random
import time

from selenium.webdriver.common.by import By

from PageClass.basePage import BasePage
from Util import logger, ZTCCLOUDError


class EcsMyApprovalPage(BasePage):
    # 单据编号
    _boeNo = (By.ID, 'undefined_boeNo')

    def getBoeNo(self):
        return self._boeNo

    def input_boeNo(self, text):
        self.send_text(text, *self._boeNo)
        logger.info("输入查询的单据编号为：{}".format(text))

    # 查询
    def click_boeNoSelectButton(self):
        self.click_button('查询')
        logger.info("点击查询按钮")

    # 查询结果(此页面已弃用)
    _boeNoSelectResult = (By.XPATH, '//*[@id="app"]//table/tbody/tr/td[2]')

    def selectResultIsOrNot(self, boeNum):
        if self.get_elementText(*self._boeNoSelectResult) == boeNum:
            logger.info("Success ， 查询出对应结果")
            return True
        else:
            logger.info("Fail ， 没有查询出对应结果")
            return False

    # 查询结果(单号)
    _boeNoSelectResultBusiness = (By.XPATH, '//*[@id="app"]//table/tbody/tr/td[3]')

    def selectResultIsOrNotBusiness(self, boeNum):
        if self.get_elementText(*self._boeNoSelectResultBusiness) == boeNum:
            logger.info("Success ， 查询出对应结果")
            return True
        else:
            logger.info("Fail ， 没有查询出对应结果")
            return False

    def click_boeNoToDetail(self):
        self.click(*self._boeNoSelectResultBusiness)
        self.switchWindow()

    # 同意
    _boeBusinessApprove = (By.ID, 'agree')

    def click_boeBusinessApprove(self):
        self.click(*self._boeBusinessApprove)
        logger.info("点击审批单据同意按钮")

    # 确定
    _boeBusinessTipConfirm = (By.XPATH, '/html/body/div[2]/div/div[3]/button[2]')

    def click_boeBusinessTipConfirm(self):
        self.click_button('确定')
        logger.info("点击确定按钮")

    # 拒绝
    _boeBusinessReject = (By.ID, 'reject')

    def click_boeBusinessReject(self):
        self.click(*self._boeBusinessReject)
        logger.info("点击审批单据拒绝按钮")

    # 退回节点
    _boeRejectNode = (By.ID, 'form_targetNodeName')

    def select_rejectNode(self, nodeName):
        self.click(*self._boeRejectNode)
        time.sleep(3)
        selectElementList = self.find_elements(*(By.XPATH, '//div[@x-placement="bottom-start"]//li'))
        flag = False
        for element in selectElementList:
            if element.text == nodeName:
                self._clickElement(element)
                flag = True
                break
        if not flag:
            raise ZTCCLOUDError(f"退回节点中未找到名称为 {nodeName} 的选项，请确认")

    # 审批意见
    _approvalComment = (By.XPATH, '//div[@class="bill-area approval"]/div[2]/div/textarea')

    def input_approvalComment(self, approvalComment):
        self.send_text(approvalComment, *self._approvalComment)

    # 直送至我
    _wfPassedToThis = (By.XPATH, '//div[@role="switch"]')

    def select_switch(self):
        isChecked = False
        while True:
            if isChecked:
                break
            switchElement = self.find_element(*self._wfPassedToThis)
            self._clickElement(switchElement)
            time.sleep(random.random())
            isChecked = switchElement.get_attribute('aria-checked')
            if isChecked:
                break

    # 点击提交按钮
    def click_submit(self):
        self._clickElement(self.find_element(*(By.XPATH, '//*[@id="form"]/div[3]/div/button[2]')))
